﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class GPSGroupsByCompanyIDSearchAndMultipleSelects : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent();
        public event AcceptEvent OnAcceptEvent;
        public SessionManager objSessionManager;

        //Propeties
        [Browsable(true), Category("Custom"), DefaultValue(true)]
                
        protected void showMessage(string msg)
        {
            RadWindowManager2.RadAlert("<center>" + msg + "</center>", 500, 100, "Departamentos", null, null);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];

            }
            catch (Exception ex)
            {
                //showMessage(ex.Message);
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSGroupsGrid);
            }
        }

        protected void btnGPSGroupsSearch_Click(object sender, EventArgs e)
        {
            GetGPSGroupsGridToAssociate();
        }

        protected void GetGPSGroupsGridToAssociate()
        {
            GPSGroupsGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.GPSGroupsProvider.GetGPSGroupsToAssociateBusRouteFind(objSessionManager.CompanyIDSelectedComboBox, txtSearchName.Text);
            GPSGroupsGrid.DataBind();
            //AdministratorsGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.GPSGroupsProvider.GetGPSGroupsToAssociateBusRoute(
        }

        protected void btnGPSGroupsOK_Click(object sender, EventArgs e)
        {

            //GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            GPSTrackingDBV2.Entities.BusRouteByGpsGroups BusRouteByGpsGroupsEntity = new GPSTrackingDBV2.Entities.BusRouteByGpsGroups();
            
            int busRouteID;

            try
            {
                busRouteID = objSessionManager.busRouteID_BusRoutesByGPSGroups;
            }
            catch (Exception ex)
            {
                showMessage("No se ha seleccionado una ruta, debe seleccionar un antes de asociar los grupos de dispositivos.");
                return;
            }
 
            foreach (Telerik.Web.UI.GridDataItem selectedItem in GPSGroupsGrid.SelectedItems)
            {
                try
                {
                    //tm.BeginTransaction();
                    BusRouteByGpsGroupsEntity.Dispose();
                    BusRouteByGpsGroupsEntity.BusRouteId = busRouteID;
                    BusRouteByGpsGroupsEntity.GpsGroupId = int.Parse(selectedItem["GpsGroupId"].Text);

                    GPSTrackingDBV2.Data.DataRepository.BusRouteByGpsGroupsProvider.Insert(BusRouteByGpsGroupsEntity);
                    //tm.Commit();
                }
                catch (Exception ex)
                {
                    //tm.Rollback();
                    //ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSGrid);
                }
            }
            GetGPSGroupsGridToAssociate();
            OnAcceptEvent();
        }
    }
}